Evaluation Metrics
OpenCLIP uses several standard metrics to evaluate model performance on zero-shot classification and retrieval tasks.Classification Metrics
Top-1 Accuracy
Top-1 accuracy is the primary metric for classification tasks. It measures the percentage of samples where the model’s highest-confidence prediction matches the ground truth label.Top-5 Accuracy
Top-5 accuracy is more lenient—it considers a prediction correct if the ground truth label appears in the model’s top 5 predictions. Formula:- Classes are visually similar (e.g., dog breeds)
- The task has high inherent ambiguity
- Comparing models that might have similar top-1 but different top-5 performance
Per-Class vs. Overall Accuracy
OpenCLIP reports overall accuracy averaged across all samples. For class-imbalanced datasets, you might also want to compute per-class accuracy and take the mean.Zero-Shot Accuracy Computation
Zero-shot accuracy in OpenCLIP is computed as follows:1. Text Classifier Construction
For each class, generate multiple text embeddings using prompt templates:2. Image Encoding
Encode the test image:3. Similarity Computation
Compute cosine similarity between image and all class embeddings:4. Accuracy Calculation
Compare predictions to ground truth:Retrieval Metrics
For image-text retrieval tasks (like Flickr30k and MSCOCO), OpenCLIP uses standard retrieval metrics:Recall@K
Recall@K measures the percentage of queries where the correct item appears in the top K retrieved results. Formula:- R@1: Strictest metric (correct item must be rank 1)
- R@5: Correct item in top 5
- R@10: Correct item in top 10
Image-to-Text Retrieval
Given an image, retrieve relevant text captions:Text-to-Image Retrieval
Given a text query, retrieve relevant images:Mean Rank
Mean rank measures the average position of the correct item in the ranked list:Aggregate Metrics
Average Performance Across Datasets
The “Average perf. on 38 datasets” metric in our results is computed as:Weighted Average
Some benchmarks use weighted averages where larger datasets have more influence:Logging Metrics
OpenCLIP automatically logs metrics to your configured logging backend during training.TensorBoard
Enable TensorBoard logging:train/loss: Training loss per steptrain/learning_rate: Current learning rateimagenet-zeroshot-val-top1: Zero-shot ImageNet top-1 accuracyimagenet-zeroshot-val-top5: Zero-shot ImageNet top-5 accuracy
Weights & Biases (wandb)
Enable wandb logging:- Real-time loss curves
- Zero-shot accuracy over time
- System metrics (GPU utilization, etc.)
For older runs (before PR #613), use the
step variable instead of Step in wandb, as the latter was not properly set.Custom Metrics
You can add custom metrics by modifying the training loop:Metric Interpretation
ImageNet Zero-Shot Accuracy
Top-1 vs Top-5 Gap
The gap between top-1 and top-5 accuracy indicates:- Small gap (< 15%): Model is confident and accurate
- Large gap (> 25%): Model often has correct answer in top 5 but not top 1, suggesting uncertainty or ambiguous classes
Cross-Dataset Performance
Strong models should maintain performance across datasets:- Consistent: Good performance across all 38 datasets
- Specialized: High performance on some datasets but lower on others
- Overfit: High ImageNet but low on distribution shift datasets
Computing Your Own Metrics
Using CLIP Benchmark
Custom Evaluation Loop
Best Practices
Next Steps
- Learn how to run zero-shot evaluation
- Explore benchmark results across 38 datasets
- See training guide for optimizing metrics
